package view;

import util.LoopMusicPlayer;
import util.MusicPlayer;
import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

public class LaunchFrame extends JFrame {

    private JLayeredPane layeredPane = new JLayeredPane();

    public LaunchFrame() {
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setTitle("植物大战僵尸");
        this.setSize(810, 625);
        this.setResizable(false);
        this.setLocationRelativeTo(null);
        this.setVisible(true);
        this.setContentPane(layeredPane);
        this.setIconImage(new ImageIcon("img\\Icon.png").getImage());

        // 下层
        ImageIcon background = new ImageIcon("img\\Surface.png");
        JPanel panel = new JPanel(){
            public void paintComponent(Graphics g) {
                super.paintComponent(g);
                g.drawImage(background.getImage()
                        , 0, 0,this.getWidth(), this.getHeight(), this);
            }
        };
        panel.setBounds(0, 0, background.getIconWidth(), background.getIconHeight());
        layeredPane.add(panel, 99);

        // 上层
        ImageIcon wood = new ImageIcon("img\\SelectorScreen_WoodSign1.png");
        final ImageIcon woodsign1 = new ImageIcon("img\\SelectorScreen_WoodSign2.png");
        final ImageIcon woodsign2 = new ImageIcon("img\\SelectorScreen_WoodSign2_press.png");
        JLabel lblnorth = new JLabel(wood);
        JLabel lbName = new JLabel("陈文杰", JLabel.CENTER);
        JTextField textName = new JTextField();
        lbName.setForeground(Color.white);
        lbName.setFont(new Font("粗体",1,20));
        textName.setHorizontalAlignment(JTextField.CENTER);
        textName.setVisible(false);
        final JLabel woodsign = new JLabel(woodsign1);
        woodsign.addMouseListener(new MouseAdapter() {
            public void mouseClicked(MouseEvent e) {
                textName.setVisible(true);
            }
            public void mouseEntered(MouseEvent e) {
                woodsign.setIcon(woodsign2);
                new Thread(new MusicPlayer("audio\\kernelpult2.mp3")).start();
                setCursor(HAND_CURSOR);
            }
            public void mouseExited(MouseEvent e) {
                woodsign.setIcon(woodsign1);
                setCursor(DEFAULT_CURSOR);
            }
        });
        textName.addMouseListener(new MouseAdapter() {
            public void mouseExited(MouseEvent e) {
                if (!"".equals(textName.getText())) {
                    lbName.setText(textName.getText());
                    textName.setVisible(false);
                }
            }
        });
        lblnorth.setBounds(20, 0, wood.getIconWidth(), wood.getIconHeight());
        woodsign.setBounds(20, 140, woodsign1.getIconWidth(),
                woodsign1.getIconHeight());
        lbName.setBounds(120, 95, 100, 20);
        textName.setBounds(120, 95, 100, 20);
        layeredPane.add(lblnorth, 0);
        layeredPane.add(woodsign, 0);
        layeredPane.add(lbName, 0);
        layeredPane.add(textName, 0);
        Thread bgm = new Thread(new LoopMusicPlayer("audio\\Mountains.mp3"));
        bgm.start();
        // 游戏模式选项层
        final ImageIcon img1 = new ImageIcon("img\\SelectorScreen_Adventure_highlight.png");
        final ImageIcon img11 = new ImageIcon("img\\SelectorScreen_StartAdventure_Highlight.png");
        ImageIcon img2 = new ImageIcon("img\\SelectorScreen_Vasebreaker_button.png");
        ImageIcon img3 = new ImageIcon("img\\SelectorScreen_Challenges_button.png");
        final JLabel lbStar = new JLabel(img1);
        lbStar.addMouseListener(new MouseAdapter() {
            public void mouseClicked(MouseEvent e) {
                //LaunchFrame.this.dispose();
                layeredPane.setVisible(false);
                bgm.stop();
                new GameView(LaunchFrame.this);
                setCursor(DEFAULT_CURSOR);
            }
            public void mouseEntered(MouseEvent e) {
                lbStar.setIcon(img11);
                new Thread(new MusicPlayer("audio\\kernelpult2.mp3")).start();
                setCursor(HAND_CURSOR);
            }
            public void mouseExited(MouseEvent e) {
                lbStar.setIcon(img1);
                setCursor(DEFAULT_CURSOR);
            }
        });
        JLabel lbl2 = new JLabel(img2);
        JLabel lbl3 = new JLabel(img3);
        lbStar.setBounds(410, 70, img1.getIconWidth(), img1.getIconHeight()+25);
        lbl2.setBounds(420, 195, img2.getIconWidth(), img2.getIconHeight());
        lbl3.setBounds(415, 295, img3.getIconWidth(), img3.getIconHeight());
        layeredPane.add(lbStar, 0);
        layeredPane.add(lbl2, 0);
        layeredPane.add(lbl3, 0);
        // 选项，帮助，退出
        final ImageIcon option1 = new ImageIcon("img\\SelectorScreen_Options1.png");
        final ImageIcon help1 = new ImageIcon("img\\SelectorScreen_Help1.png");
        final ImageIcon quit1 = new ImageIcon("img\\SelectorScreen_Quit1.png");
        final ImageIcon option2 = new ImageIcon("img\\SelectorScreen_Options2.png");
        final ImageIcon help2 = new ImageIcon("img\\SelectorScreen_Help2.png");
        final ImageIcon quit2 = new ImageIcon("img\\SelectorScreen_Quit2.png");
        final JLabel option = new JLabel(option1);
        final JLabel help = new JLabel(help1);
        final JLabel quit = new JLabel(quit1);
        option.setBounds(565, 490, option1.getIconWidth(), option1.getIconHeight());
        help.setBounds(655, 520, help1.getIconWidth(), help1.getIconHeight());
        quit.setBounds(725, 510, quit1.getIconWidth(), quit1.getIconHeight());
        option.addMouseListener(new MouseAdapter() {
            public void mouseClicked(MouseEvent e) {
                // TODO Auto-generated method stub
            }
            public void mouseEntered(MouseEvent e) {
                option.setIcon(option2);
                new Thread(new MusicPlayer("audio\\kernelpult2.mp3")).start();
                setCursor(HAND_CURSOR);
            }
            public void mouseExited(MouseEvent e) {
                option.setIcon(option1);
                setCursor(DEFAULT_CURSOR);
            }
        });
        help.addMouseListener(new MouseAdapter() {
            public void mouseClicked(MouseEvent e) {
                // TODO Auto-generated method stub

            }
            public void mouseEntered(MouseEvent e) {
                help.setIcon(help2);
                setCursor(HAND_CURSOR);
                new Thread(new MusicPlayer("audio\\kernelpult2.mp3")).start();
            }
            public void mouseExited(MouseEvent e) {
                help.setIcon(help1);
                setCursor(DEFAULT_CURSOR);
            }
        });
        quit.addMouseListener(new MouseAdapter() {
            public void mouseClicked(MouseEvent e) {
                System.exit(0);

            }
            public void mouseEntered(MouseEvent e) {
                quit.setIcon(quit2);
                setCursor(HAND_CURSOR);
                new Thread(new MusicPlayer("audio\\kernelpult2.mp3")).start();
            }
            public void mouseExited(MouseEvent e) {
                quit.setIcon(quit1);
                setCursor(DEFAULT_CURSOR);
            }
        });
        layeredPane.add(option, 0);
        layeredPane.add(help, 0);
        layeredPane.add(quit, 0);
    }
}